from research.calcor.pos import original_close
class MA_ATR_close(original_close):
    def __init__(self,name="",ma="MA",atr="ATR",value=[1.5,2]):
        self.ma=ma
        self.atr=atr
        self.value=value
        if name=="":
            self.name = type(self).__name__
        else:
            self.name = type(self).__name__+"("+str(name)+")"
    def reset(self,value):
        self.value=value
    def onbar(self,ss,b):
        if ss.pos=="buy":
            t1 = None
            t2=None
            if b[self.ma] and b[self.atr]:
                p0=self.value[0]
                if p0 is not None:
                    t1=b[self.ma] -p0* b[self.atr]
                    if t1>b["close"]:
                        order = {"obj": b["obj"], "timekey": b["timekey"], "price": b["close"], "action": "closesell",
                                 "info": self.name+"止损" + str(b[self.ma])+"-"+str(p0)+"*"+str(b[self.atr])}
                        ss.signals_data.append(order)
                        ss.pos = None
                        return True,"closesell","stop_loss",t1, None
                p0 = self.value[1]
                t2=None
                if p0   is not None:
                    t2 = b[self.ma] + p0 * b[self.atr]
                    if t2 < b["close"]:
                        order = {"obj": b["obj"], "timekey": b["timekey"], "price": b["close"], "action": "closesell",
                                 "info": self.name+"止赢" + str(b[self.ma]) + "+" + str(p0) + "*" + str(b[self.atr])}
                        ss.signals_data.append(order)
                        ss.pos = None
                        return True, "closesell","stop_gain",t1, t2
            return False,"","",t1,t2
        elif ss.pos=="sell":
            t1 = None
            t2 = None
            if b[self.ma] and b[self.atr]:
                p0 = self.value[0]
                if p0   is not None:
                    t1 = b[self.ma] + p0 * b[self.atr]
                    if t1 < b["close"]:
                        order = {"obj": b["obj"], "timekey": b["timekey"], "price": b["close"],
                                 "action": "closebuy",
                                 "info": self.name+"止损" + str(b[self.ma]) + "+" + str(p0) + "*" + str(b[self.atr])}
                        ss.signals_data.append(order)
                        ss.pos = None
                        return True, "closebuy","stop_loss",t1,None
                p0 = self.value[1]
                if p0   is not None:
                    t2 = b[self.ma] - p0 * b[self.atr]
                    if t2 > b["close"]:
                        order = {"obj": b["obj"], "timekey": b["timekey"], "price": b["close"],
                                 "action": "closebuy",
                                 "info": self.name+"止赢" + str(b[self.ma]) + "-" + str(p0) + "*" + str(b[self.atr])}
                        ss.signals_data.append(order)
                        ss.pos = None
                        return True,"closebuy", "stop_gain",t1,t2
            return False, "", "",t1,t2
        return False,"","",None,None